package com.sbl.bajw.common.web;

import org.apache.log4j.Logger;

import com.baidu.yun.channel.auth.ChannelKeyPair;
import com.baidu.yun.channel.client.BaiduChannelClient;
import com.baidu.yun.channel.model.PushUnicastMessageRequest;
import com.baidu.yun.channel.model.PushUnicastMessageResponse;
import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;
import com.sbl.bajw.util.LogUtil;

public class BaiduMsgPush implements Runnable {
	
	static String apiKey = "X1j8IIXvLlbWg1eP3K9jdmww";
	static String secretKey = "zfEWjXBLuYczO1xIDMGWotxwWx58CQ8W";
	static String url = "http://channel.api.duapp.com/rest/2.0/channel/channel";
	static Logger log = null;
	String userid = null;
	String channelid = null;
	String content = null;
	String title = null;
	
	static{
		log = LogUtil.getLogger("msgpush");
	}
	
	public BaiduMsgPush(String userid, String channelid,String content,String title){
		this.userid = userid;
		this.channelid = channelid;
		this.content = content;
		this.title = title;
	}
	
	public static void msgPush(String userid, String channelid,String content,String title){
		if(userid == null || channelid == null ){
			return;
		}
		userid = userid.trim();
		channelid = channelid.trim();
		
		if(userid.equals("") || channelid.equals("")){
			return;
		}
		
		Thread runer = new Thread(new BaiduMsgPush(userid, channelid, content, title));
		runer.start();
	}	
	
	public void execute(String userid, String channelid,String content,String title){
		/*
         * @brief 推送单播通知(Android Push SDK拦截并解析) message_type = 1 (默认为0)
         */

        // 1. 设置developer平台的ApiKey/SecretKey
        ChannelKeyPair pair = new ChannelKeyPair(apiKey, secretKey);

        // 2. 创建BaiduChannelClient对象实例
        BaiduChannelClient channelClient = new BaiduChannelClient(pair);
        
        channelClient.setChannelLogHandler(new YunLogHandler() {
            @Override
            public void onHandle(YunLogEvent event) {
            	System.out.println(event.getMessage());
            }
        });

        try {

            // 4. 创建请求类对象
            // 手机端的ChannelId， 手机端的UserId， 先用1111111111111代替，用户需替换为自己的
        	String msgContent = "";
            PushUnicastMessageRequest request = new PushUnicastMessageRequest();
            request.setDeviceType(3); // device_type => 1: web 2: pc 3:android
            request.setChannelId(Long.parseLong(channelid));
            request.setUserId(userid);
            request.setMessageType(1);
            msgContent += "{";
            msgContent += "\"title\":\""+title+"\"";
            msgContent += ",\"description\":\""+content+"\"";
            msgContent += ",\"notification_builder_id\":0";
            msgContent += ",\"notification_basic_style\":3";
            msgContent += ",\"open_type\":2";
            msgContent += "}";
            request.setMessage(msgContent);

            // 5. 调用pushMessage接口
            PushUnicastMessageResponse response = channelClient
                    .pushUnicastMessage(request);
            if(log != null ){
            	 log.info("推送成功,userid:"+userid+",channelid"+channelid+",发送客户端个数："+response.getSuccessAmount()+",requestid:");
            }
           
            System.out.println(response.getSuccessAmount());

        } catch (Exception e) {
            // 处理客户端错误异常
            e.printStackTrace();
            log.error("推送异常:", e);
        } 
	}

	@Override
	public void run() {
		execute(userid, channelid, content, title);
	}
}
